function final_compression_test(input_folder_path,folder_Q75,output_folder_path,output_doulble,desired_compression_ratio,desired_compression_technique,Quality,entension)




%I have to compressed the images according to desired compression ratio.
%But the the problem is that there is not compession parameter to compress
%direclty. So What I did is that I controlled the compression ratio by
%changing the quality of the image. More the quality of image, less is the
%compression and vice versa.

% I started with random quality e.g 55 and then increased or decreased the
% quality depending on the compression requirement.

% May be my code is not optimized but atleast it works correctly..   :)


clc;
close all;



% Input Folder

 a=input_folder_path;
 Q=folder_Q75;
 
 mkdir(Q);
 
 % Output Folder 
 c=output_folder_path;
 
 du=output_doulble;
 d=dir([a ['*.' entension]]);
 
 mkdir(du);
 
 mkdir(c);
 
  t= strcmp(desired_compression_technique,'jpg');
   w=strcmp(desired_compression_technique,'jp2');
   z=strcmp(desired_compression_technique,'jxr');
    
 

  if t == 1     
    for i=[1:numel(d)]
        org=imread([a d(i).name]); 
       % org=imread('004.tiff');

       img_new_name= strrep([c d(i).name],['.' entension],'.jpg');


       % org=imread('004.tiff');
        imwrite(org,img_new_name, desired_compression_technique , 'quality' , 55);
        K=imfinfo(img_new_name , desired_compression_technique);
        %K=imfinfo('ab.jpg');
        K1=imfinfo([a,d(i).name],entension);

        % if image is already compressed then    
        %image_byte = K1.FileSize;

        % If Image is not compressed then size of uncompressed Image
        image_byte = K1.Width*K1.Height*K1.BitDepth/8;

        %Size of compressed Image
        compressed_byte=K.FileSize;
        CR =  image_byte /compressed_byte;




        % as we need required compression so that we can set the value of CR
        % accordingly. As my case I need compression 10. So my CR is between 11
        % and 10. If we want to make CR=10 then we hvae to decrease the step
        % size and then matlab will take a lot of time.

        for m=[1:1:44]
            if CR > desired_compression_ratio           

                imwrite(org,img_new_name , desired_compression_technique,'quality', 55+m);

                K=imfinfo(img_new_name, desired_compression_technique);         
                compressed_byte=K.FileSize;

                CR =  image_byte /compressed_byte;


            end
        end 

        for n=[1:1:54]
            if CR < desired_compression_ratio-1
                imwrite(org,img_new_name , desired_compression_technique , 'quality' , 55-n);            
                K=imfinfo(img_new_name, desired_compression_technique);

                compressed_byte=K.FileSize;

                CR =  image_byte /compressed_byte;


            end
          end
      end
 
 elseif z == 1                
         
       for i=[1:numel(d)]
               %S org=imread([a d(i).name]); 
               % org=imread('004.tiff');
               img_new_name= strrep([c d(i).name],['.' entension],'.jxr');

               f=55;
              system( ['JxrEncApp -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(f)] ) ;




           % org=imread('004.tiff');
           % imwrite(org,img_new_name, 'jpg' , 'quality' , 55);
            K=dir(img_new_name);
            %K=imfinfo('ab.jpg');


            K1=imfinfo([a,d(i).name], entension);

            % if image is already compressed then    
            %image_byte = K1.FileSize;

            % If Image is not compressed then size of uncompressed Image
            image_byte = K1.Width*K1.Height*K1.BitDepth/8;

            %Size of compressed Image

            compressed_byte=K.bytes;
            CR =  image_byte /compressed_byte;




            % as we need required compression so that we can set the value of CR
            % accordingly. As my case I need compression 10. So my CR is between 11
            % and 10. If we want to make CR=10 then we hvae to decrease the step
            % size and then matlab will take a lot of time.

            if CR >desired_compression_ratio
                for f=[1:1:44]
                     if CR >desired_compression_ratio   

                          system( ['JxrEncApp -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(55-f)] );
                         K=dir(img_new_name);
                        compressed_byte=K.bytes;

                              
                        CR =  image_byte /compressed_byte;


                     end
                end 
            end 
            if CR <desired_compression_ratio-1
                for f=[1:1:54]
                    if CR < desired_compression_ratio-1

                          system( ['JxrEncApp -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(f+45)] );

                         K=dir(img_new_name);
                        compressed_byte=K.bytes;
                        CR =  image_byte /compressed_byte;




                    end
                end
            end 


       end 
  
 else 
        
      for i=[1:numel(d)]
            org=imread([a d(i).name]); 
                       % org=imread('004.tiff');
             img_new_name= strrep([c d(i).name],['.' entension],'.jpg');
                       % org=imread('004.tiff');
              imwrite(org,img_new_name, 'jp2' , 'CompressionRatio' , desired_compression_ratio);
              
                        %K=imfinfo(img_new_name , 'jpg');
              
              
      end 
      
  end
  
  tmp_folder=tempdir;
for i=[1:numel(d)]
        org=imread([a d(i).name]);        
        

       img_new_name= strrep([Q d(i).name],['.' entension] ,'.jpg');
        %img_new_name1= strrep([tmp_folder d(i).name],'.jpg','.tif');


       % org=imread('004.tiff');
        imwrite(org,img_new_name, 'jpg' , 'quality' , Quality);
        
        %K=imfinfo(img_new_name , desired_compression_technique);
end
  
if t == 1      
      equal_size_using_jpeg(Q,du,c)
   
      
  
elseif z ==1 
    %====================================================================================
          d=dir([Q ['*.jpg']])      
 %
        for i=[1:numel(d)]
           org=imread([Q d(i).name]); 
    %        
     %  
           img_new_name= strrep([Q d(i).name],'.jpg','.bmp');
           imwrite(org,img_new_name,'bmp');
             
        end 
         
        
  %====================================================================================

           % org=imread('004.tiff')
       jpeg_xr(Q,du,c)
      
else
        %====================================================================================
          
        
       jp2k(Q,du,c)
      
       %====================================================================================  
 end 
  
end 
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
 
